<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .child {
            border: 1px solid red;
        }
        
        .parent {
            border: 1px solid blue;
            width: 300px;
            padding: 30px;
        }
    </style>
</head>

<body>
    <!-- 如果 父组件与子组件之间 数据是相互影响的 那么更新了父组件 或者 子组件中的数据  钩子函数触发顺序是
        父 beforeUpdate ——> 子 beforeUpdate ——> 子 updated  ——> 父 updated
    -->
    <div id="app">
        <div class="parent">
            父组件的内容: {{msg}} <input type="text" v-model="msg">
            <child-com @change="msg=$event" :msg="msg"></child-com>
        </div>
        <button @click="$destroy()">销毁父组件</button>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.9/vue.js"></script>
    <script>
        Vue.component('child-com', {
            template: `<div class="child">我是子组件
                    子组件的内容: {{cmsg}}
                    <input type="text" v-model="cmsg" @input="$emit('change', cmsg)">
                    <button @click="$destroy()">销毁子组件</button>
                </div>`,
            props: {
                msg: String
            },
            data() {
                return {
                    cmsg: this.msg
                }
            },
            watch: {
                msg(val) {
                    this.cmsg = val
                }
            },
            beforeCreate() {
                console.log('子组件 beforeCreate');
            },
            created() {
                console.log('子组件 created');
            },
            beforeMount() {
                console.log('子组件 beforeMount');
            },
            mounted() {
                console.log('子组件 mounted');
            },
            beforeUpdate() {
                console.log('子组件 beforeUpdate');
            },
            updated() {
                console.log('子组件 updated');
            },
            beforeDestroy() {
                console.log('子组件 beforeDestroy');
            },
            destroyed() {
                console.log('子组件 destroyed');
            },
        })

        var vm = new Vue({
            el: "#app",
            data: {
                msg: 'hello',
            },
            beforeCreate() {
                console.log('父组件 beforeCreate');
            },
            created() {
                console.log('父组件 created');
            },
            beforeMount() {
                console.log('父组件 beforeMount');
            },
            mounted() {
                console.log('父组件 mounted');
            },
            beforeUpdate() {
                console.log('父组件 beforeUpdate');
            },
            updated() {
                console.log('父组件 updated');
            },
            beforeDestroy() {
                console.log('父组件 beforeDestroy');
            },
            destroyed() {
                console.log('父组件 destroyed');
            },
        })
    </script>
</body>

</html>